package com.canyou.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.canyou.api.BaseApiController;
import com.canyou.api.ResponseBO;
import com.canyou.system.model.OperLog;
import com.canyou.system.service.OperLogService;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.canyou.api.ResponseBO.ok;

/**
 * 操作日志前端控制器
 *
 * @author canyou
 * @since 2019-05-14
 */
@Api(tags = "操作日志")
@ApiSort(1006)
@RestController
@RequestMapping("/oper_log")
@Validated
public class OperLogController extends BaseApiController {

    @Autowired
    private OperLogService operLogService;

    @ApiOperation(value = "获取操作日志分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", paramType = "query", value = "当前第几页",
                    dataType = "int", example = "1"),
            @ApiImplicitParam(name = "pageSize", paramType = "query", value = "每页记录数",
                    dataType = "int", example = "10"),
            @ApiImplicitParam(name = "module", paramType = "query", value = "模块名称",
                    dataType = "string"),
            @ApiImplicitParam(name = "operation", paramType = "query", value = "操作内容",
                    dataType = "string"),
            @ApiImplicitParam(name = "responseCode", paramType = "query", value = "操作状态",
                    dataType = "int", example = "1"),
            @ApiImplicitParam(name = "username", paramType = "query", value = "用户名称",
                    dataType = "string"),
            @ApiImplicitParam(name = "startTime", paramType = "query", value = "开始时间（yyyy-MM-dd HH:mm:ss）",
                    dataType = "datetime"),
            @ApiImplicitParam(name = "endTime", paramType = "query", value = "结束时间（yyyy-MM-dd HH:mm:ss）",
                    dataType = "datetime")
    })
    @PreAuthorize("hasPerm('operlog')")
    @GetMapping("/get_page")
    public ResponseBO<IPage<OperLog>> getOperLogPage(@RequestParam(defaultValue = PAGE_INDEX, required = false) int pageNumber,
                                                     @RequestParam(defaultValue = PAGE_SIZE, required = false) int pageSize,
                                                     OperLog operLog) {
        IPage<OperLog> page = new Page<>(pageNumber, pageSize);
        IPage<OperLog> operLogList = operLogService.getOperLogPage(page, operLog);
        return ok(operLogList);
    }

    @ApiOperation(value = "获取操作日志列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "module", paramType = "query", value = "模块名称",
                    dataType = "string"),
            @ApiImplicitParam(name = "operation", paramType = "query", value = "操作内容",
                    dataType = "string"),
            @ApiImplicitParam(name = "responseCode", paramType = "query", value = "操作状态",
                    dataType = "int", example = "1"),
            @ApiImplicitParam(name = "username", paramType = "query", value = "用户名称",
                    dataType = "string"),
            @ApiImplicitParam(name = "startTime", paramType = "query", value = "开始时间（yyyy-MM-dd HH:mm:ss）",
                    dataType = "datetime"),
            @ApiImplicitParam(name = "endTime", paramType = "query", value = "结束时间（yyyy-MM-dd HH:mm:ss）",
                    dataType = "datetime")
    })
    @PreAuthorize("hasPerm('operlog')")
    @GetMapping("/get_list")
    public ResponseBO<List<OperLog>> getOperLogList(OperLog operLog) {
        List<OperLog> list = operLogService.getOperLogList(operLog);
        return ok(list);
    }

}
